﻿var msgRange="输入数据超出允许范围";
var msgRangeEN="\nInPut is out of bound"
var msgRangeMin="，必须大于等于";
var msgRangeMinEN = ", must be more than or equal with";
var msgRangeMax="，必须小于等于";
var msgRangeMaxEN = ", must be less than or equal with";
var msgDate="输入数据不是有效的日期\nInput is an invalid date.";
var msgNumber="输入数据不是有效的数值";
var msgNumberEN="\nInput is an invalid numerical value"
var msgDecimal="，小数点后尾数不能超过{0}位";
var msgDecimalEN = ",there should be no more than {0} bit(s) after the radix point.";
var msgData="输入数据不是允许的数据\nInput is not allowed.";
var msgEmail="输入的不是正确的邮件地址\nInput email address is invalid.";
var msgZip="输入的不是正确的邮政编码";
var msgTime="输入数据不是有效的时间\nInput date is invalid.";
var msgPwd="密码必须在1位到20位之间";
var msgPhone="输入的不是正确的电话/传真号码";
var msgCitizenCode="输入的不是正确的身份证号码";
var msgMobilePhone="输入的不是正确的手机号码";

var msgDateRange="开始时间不能大于结束时间.\nStart Date can not be greater than End Date"
var msgDateRange_From="结束时间不能小于开始时间.\nEnd Date can not be less than Start Date"


var msgSuccess="success";
var msgError="错误\nerror";
var msgUnknow="发生未知错误\n Unknown error";
var msgEmpty="输入数据不能为空\n Input should not be blank.";
var msgShort="输入数据不符合长度要求";
var msgUninit="输入元件尚未准备就绪\nComponents are not prepared.";
var msgUnReady="输入校验功能尚未就绪\nChecking function is not prepared.";

/**
 * 设置窗口状态，bSafe为true时为一般状态，false为安全状态，sMsg是安全状态时的提示信息
**/
function window.setState(bSafe, sMsg){
  if (window.master==null || window.master.readyState!="complete") return;
  window.master.setState(bSafe, sMsg);
}

/**
 * 取得最近的指定TagName的父节点
**/
function window.getParentByTagName(oElement,sTagName){
  var oParent=oElement;
  while (oParent!=null) {
    if (oParent.tagName.toLowerCase()==sTagName.toLowerCase()) {
      if (oParent.readyState=="complete") return oParent;
      break;
    }
    oParent=oParent.parentElement;
  }
  return null;
}

//数据验证

function checkData(oDataSrc){
  var sValue=oDataSrc.value;
  var sXmlSrcId=oDataSrc.getAttribute("src");
  if (sXmlSrcId==null) return;
  var oXmlSrc=window.document.getElementById(sXmlSrcId);
  if (oXmlSrc==null) return;
  var arrItems=oXmlSrc.XMLDocument.documentElement.childNodes;
  for (var m=0;m<arrItems.length;m++) {
    if (sValue==arrItems[m].getAttribute("value")) return msgSuccess;
  }
  return msgData;
}

function checkNumberLength(oDataSrc){
	var sResult = msgData;
	var sLength = oDataSrc.getAttribute("length");
	if (sLength!=null && sLength!=""){
		var iLength = parseInt(sLength);
		var sValue=oDataSrc.value;
		var sValueR=sValue.replace(/\D+/,"");
		if (sValue.length==sValueR.length && sValueR.length==iLength){
			sResult = msgSuccess;
		}

	}
	return sResult;

}

function checkNumberLengthMax(oDataSrc){
	var sResult = msgData;
	var sLength = oDataSrc.getAttribute("length");
	if (sLength!=null && sLength!=""){
		var iLength = parseInt(sLength);
		var sValue=oDataSrc.value;
		var sValueR=sValue.replace(/\D+/,"");
		if (sValue.length==sValueR.length && sValueR.length<=iLength){
			sResult = msgSuccess;
		}

	}
	return sResult;

}

function checkDate(oDataSrc){
  var sValue=oDataSrc.value;
  sValue=sValue.replace(/\s/g,"");
  var regDate=new RegExp("^\\d{4}\\D{1}\\d{1,2}\\D{1}\\d{1,2}$");
  if (!regDate.test(sValue)) return msgDate;

  sValue=sValue.replace(/\D/g,"-");

  var arrDate=getRealDate(sValue);
  oDataSrc.value=arrDate.join("-");

  var iTempYear=parseInt(arrDate[0]);
  var iTempMonth=parseInt(arrDate[1].replace(/^0/,""))-1;
  var iTempDay=parseInt(arrDate[2].replace(/^0/,""));

  var oRealDate=new Date(iTempYear,iTempMonth,iTempDay);
  if (oRealDate.getMonth()!=iTempMonth || oRealDate.getDate()!=iTempDay) return msgDate;

  var sMaxDate=oDataSrc.getAttribute("todate");
  if (sMaxDate!=null) {
    var arrMaxDate=getRealDate(sMaxDate);
    if (getDateInt(arrDate)>getDateInt(arrMaxDate)) return msgRange+msgRangeEN;
  }
  var sMinDate=oDataSrc.getAttribute("fromdate");
  if (sMinDate!=null) {
    var arrMinDate=getRealDate(sMinDate);
    if (getDateInt(arrDate)<getDateInt(arrMinDate)) return msgRange+msgRangeEN;
  }
  
  var sMaxDateControl=oDataSrc.getAttribute("todatecontrol");
  if (sMaxDateControl!=null) {
    
    var MaxDateControl=document.getElementById(sMaxDateControl);
    if (MaxDateControl!=null)
    {
        var arrMaxDateControl= getRealDate(MaxDateControl.value);
        if (getDateInt(arrDate)>getDateInt(arrMaxDateControl)) return msgDateRange;
    }
  }
  
  
  var sMaxDateControl_from=oDataSrc.getAttribute("fromdatecontrol");
  if (sMaxDateControl_from!=null) {
    
    var MaxDateControl_from=document.getElementById(sMaxDateControl_from);
    if (MaxDateControl_from!=null)
    {
        var arrMaxDateControl_from= getRealDate(MaxDateControl_from.value);
        if (getDateInt(arrDate)<getDateInt(arrMaxDateControl_from)) return msgDateRange_From;
    }
  }
  return msgSuccess;
}


function checkNumber(oDataSrc){
  var sValue=oDataSrc.value;
  var sPrefix="";
  var sTemp=sValue.charAt(0);
  if (sTemp=="+" || sTemp=="-") {
    sPrefix=sTemp;
    sValue=sValue.replace(/^[\+-]/,"");
  }

  sValue=sValue.replace(/^(0(?=\d))+/,"");

  var sDecimal=oDataSrc.getAttribute("decimal");
  if ( sDecimal==null) sDecimal="0";

  var regNum;
  if (sDecimal == "0") regNum=new RegExp("^\\d+$");
  else regNum=new RegExp("^\\d+\\.?\\d{0,"+sDecimal+"}$");
  
  if (!regNum.test(sValue)) return msgNumber+msgDecimal.replace("{0}",sDecimal)+msgNumberEN+msgDecimalEN.replace("{0}",sDecimal);

  sValue=sValue.replace(/^\./,"0.");
  sValue=sValue.replace(/\.$/,".0");
  sValue=sPrefix+sValue;

  var sMax=oDataSrc.getAttribute("max");
  if (sMax!=null) {
    if (parseFloat(sValue)>parseFloat(sMax)) return msgRange+msgRangeMax+sMax+msgRangeEN+msgRangeMaxEN+sMax;
  }
  var sMin=oDataSrc.getAttribute("min");
  if (sMin!=null) {
    if (parseFloat(sValue)<parseFloat(sMin)) return msgRange+msgRangeMin+sMin+msgRangeEN+msgRangeMinEN+sMin;
  }
  oDataSrc.value=sValue;
  return msgSuccess;
}

function checkNumberNoEqual(oDataSrc){
  var sValue=oDataSrc.value;
  var sPrefix="";
  var sTemp=sValue.charAt(0);
  if (sTemp=="+" || sTemp=="-") {
    sPrefix=sTemp;
    sValue=sValue.replace(/^[\+-]/,"");
  }

  sValue=sValue.replace(/^(0(?=\d))+/,"");

  var sDecimal=oDataSrc.getAttribute("decimal");
  if ( sDecimal==null) sDecimal="0";

  var regNum;
  if (sDecimal == "0") regNum=new RegExp("^\\d+$");
  else regNum=new RegExp("^\\d+\\.?\\d{0,"+sDecimal+"}$");

  if (!regNum.test(sValue)) return msgNumber+msgNumberEN;

  sValue=sValue.replace(/^\./,"0.");
  sValue=sValue.replace(/\.$/,".0");
  sValue=sPrefix+sValue;

  var sMax=oDataSrc.getAttribute("max");
  if (sMax!=null) {
    if (parseFloat(sValue)>=parseFloat(sMax)) return msgRange+msgRangeEN;
  }
  var sMin=oDataSrc.getAttribute("min");
  if (sMin!=null) {
    if (parseFloat(sValue)<=parseFloat(sMin)) return msgRange+msgRangeEN;
  }
  oDataSrc.value=sValue;
  return msgSuccess;
}


function checkEmail(oDataSrc){
  var sValue=oDataSrc.value.replace(/\s/g,"");
  var regEmail=new RegExp("^([\\w\\d\\-\\.]+)@{1}(([\\w\\d\\-]{1,67})|([\\w\\d\\-]+\\.[\\w\\d\\-]{1,67}))\\.(([a-zA-Z\\d]{2,4})(\\.[a-zA-Z\\d]{2})?)$");
  if (!regEmail.test(sValue)) return msgEmail;
  oDataSrc.value=sValue;
  return msgSuccess;
}

function checkZipCode(oDataSrc){
  var sValue=oDataSrc.value.replace(/\D/ig,"");
  if (sValue.length!=6) return msgZip;
  return msgSuccess;
}

function checkTime(oDataSrc){
  var sValue=oDataSrc.value;

  var regTime=new RegExp("^\\d{1,2}\\D{1}\\d{1,2}\\D{1}\\d{1,2}$");
  if (!regTime.test(sValue)) return msgTime;

  sValue=sValue.replace(/\D/g,"-");

  var arrDate=sValue.split("-");

  var iTempHour=parseInt(arrDate[0]);
  var iTempMinute=parseInt(arrDate[1]);
  var iTempSecond=parseInt(arrDate[2]);

  oDataSrc.value=getDbNum(arrDate[0])+":"+getDbNum(arrDate[1])+":"+getDbNum(arrDate[2]);

  if(iTempHour > 23 || iTempMinute > 59 || iTempSecond > 59) return msgTime;

  return msgSuccess;
}

function checkPassword(oDataSrc){
	var sLength = oDataSrc.value.length;
	if(sLength==null || sLength<1 || sLength>20){
		return msgPwd;
	}
	return msgSuccess;
}

function checkPhoneNumber(oDataSrc){
  var sValue = oDataSrc.value.replace(/\D/ig,"");
  if (sValue.length<7) {
    return msgPhone;
  }
  return msgSuccess;
}

function checkCitizenCode(oDataSrc){
  var sLength = oDataSrc.value.length;
  if(sLength == 15 || sLength == 18) {
  	 return msgSuccess;
  }
  return msgCitizenCode;
}

function checkMobilePhone(oDataSrc){
  var sValue = oDataSrc.value.replace(/\D/ig,"");
  if (sValue.Length == 11) return msgSuccess;
  return msgMobilePhone;
}
//  Private Methods . Do not modify them --------------------------------


function getRealDate(sRange){
  var arrRange;
  if (sRange=="today") {
    var oDate=new Date();
    arrRange=new Array((oDate.getFullYear()).toString(),oDate.getMonth()+1,oDate.getDate());
  }
  else arrRange=sRange.split("-");
  arrRange[1]=getDbNum(arrRange[1]);
  arrRange[2]=getDbNum(arrRange[2]);
  return arrRange;
}


function getDateInt(arrRange){
  return (parseInt(arrRange.join("")));
}


function getDbNum(iNum){
  var sNum=iNum.toString();
  if (sNum.length==1) return ("0"+sNum);else return sNum;
}



function checkbadwords(textcontrol) 
	{
	        var value = textcontrol.value;
	        
			var badwords = "%*'\"&<>\\";
			if (!check(value, badwords))
			{
			    return "输入中包含非法字符(%*'\"&<>\\).";
				//alert("输入中包含非法字符(%*'\"&<>\\).");
				//textcontrol.focus();
			}
			return msgSuccess;
		}
		
		
		function check(str, badwords) {
			if (typeof (str) != "string" || typeof (badwords) != "string")
			 {
			return false;
			}
			for (i=0; i<badwords.length; i++) {
			bad = badwords.charAt(i);
			for (j=0; j<str.length; j++) {
			if (bad == str.charAt(j)) {
				return false;
			break;
			}	
			}
			}
			return true;
		}
 